
package com.joysuch.wwyt.util;

import org.springframework.util.DigestUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.security.MessageDigest;

public class Md5Util {

    private Md5Util() {
    }

    public static void main(String[] args) {
        // System.out.println(md5(password:nonce:md5(public_method:url)));
        // System.out.println(md5(md5(username:realm:password):nonce:md5(public_method:url)));
        System.out.println(md5(md5("admin:1868cb647d09:Seekcy2015") + ":310421f80a50fe6b3169c8778927ee8f:"
                + md5("DESCRIBE:rtsp://192.168.0.196:554/Streaming/Channels/101?transportmode=unicast")));
        System.out.println(md5("Seekcy2015:310421f80a50fe6b3169c8778927ee8f:"
                + md5("DESCRIBE:rtsp://192.168.0.196:554/Streaming/Channels/101?transportmode=unicast")));
        System.out.println(md5(md5("123456")));
        // System.out.println(md5("bbs@8081_admin"));
        /*
         * System.out.println(md5(md5("123456"),"8b671975b6274bd68c0c79056f3c8e05"));
         * System.out.println(md5(md5("open@80_licmgr"),"00aec49b-c314-462b-9ab6-cd720f7f58fa"));
         * System.out.println(md5("Seekcy2015","WEB_LOCATE"));
         * System.out.println(md5("Seekcy2015","ADMIN_PRIV"));
         */
        /*
         * System.out.println(md5("Seekcy2016"));
         * System.out.println(md5("123456"));
         */
        // System.out.println(md5("123456"));
        String md5 = md5("baisj123");
        System.out.println(md5);
    }

    public static final String md5(String src, String salt) {
        String toEncode = src + ((salt == null) ? "" : ("{" + salt + "}"));
        char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
        try {
            byte[] btInput = toEncode.getBytes();
            // 获得MD5摘要算法的 MessageDigest 对象
            MessageDigest mdInst = MessageDigest.getInstance("MD5");
            // 使用指定的字节更新摘要
            mdInst.update(btInput);
            // 获得密文
            byte[] md = mdInst.digest();
            // 把密文转换成十六进制的字符串形式
            int j = md.length;
            char str[] = new char[j * 2];
            int k = 0;
            for (int i = 0; i < j; i++) {
                byte byte0 = md[i];
                str[k++] = hexDigits[byte0 >>> 4 & 0xf];
                str[k++] = hexDigits[byte0 & 0xf];
            }
            return new String(str);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public final static String md5(String s) {
        return md5(s, null);
    }

    public static String getMd5ByFile(File file) {
        String value = null;
        FileInputStream in = null;
        try {
            in = new FileInputStream(file);
            MappedByteBuffer byteBuffer = in.getChannel().map(FileChannel.MapMode.READ_ONLY, 0, file.length());
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            md5.update(byteBuffer);
            BigInteger bi = new BigInteger(1, md5.digest());
            value = bi.toString(16);
            value = StringUtil.leftPad(value, 32, '0');
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != in) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return value;
    }

    public static String getMd5ByFile(InputStream stream) {
        String value = null;
        FileInputStream in = null;
        try {
            byte[] cache = new byte[4096];
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            int length = 0;
            while ((length = stream.read(cache)) != -1) {
                md5.update(cache, 0, length);
            }
            BigInteger bi = new BigInteger(1, md5.digest());
            value = bi.toString(16);
            value = StringUtil.leftPad(value, 32, '0');
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != in) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return value;
    }

    public static String springMd5(String source){
        return DigestUtils.md5DigestAsHex(source.getBytes());
    }

    /**
     * 2次md5 全部大写 返回
     */
    public static String spring2Md5(String source){
        return springMd5(springMd5(source)).toUpperCase();
    }

}
